#ifndef CIRCULAR_BUFFER_H
#define CIRCULAR_BUFFER_H

#include <stdint.h>

typedef uint32_t (*ReadCallback)(void *buffer, uint32_t length);
#define SAFE_INTERVAL (0)

typedef struct {
  uint8_t *data;          /* Base address of the buffer */
  uint32_t capacity;       /* Total capacity in bytes */
  uint32_t length;        /* Current data length in bytes */
  uint32_t position;      /* Current read/write position */
  ReadCallback callback;  /* Optional read callback */
} MemoryHandle;

typedef struct {
  uint8_t *data;          /* Base address of the circular buffer */
  uint32_t capacity;      /* Total capacity in bytes */
  uint32_t write_pos;     /* Current write position */
  uint32_t read_pos;      /* Current read position */
} CircularBufferContext;

/* Function prototypes */
MemoryHandle *init_circular_buffer(uint32_t size);
void uninit_circular_buffer(MemoryHandle *handle);
uint32_t write_to_circular_buffer(void *buffer, uint32_t size, uint32_t count, void *handle);
uint32_t read_from_circular_buffer(void *buffer, uint32_t size, uint32_t count, void *handle);
int32_t circular_buffer_available_space(void *handle);
int32_t circular_buffer_used_space(void *handle);
void reset_circular_buffer(void *handle);
void circular_buffer_read_callback(MemoryHandle *handle, ReadCallback callback);
void remove_circular_buffer_read_callback(MemoryHandle *handle);

#endif /* CIRCULAR_BUFFER_H */